<?php

namespace App\Models;

use App\Models\Category\Category;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jedrzej\Pimpable\PimpableTrait;

class Article extends Model
{
    use PimpableTrait;
    use HasFactory;
    use SoftDeletes;

    /**
     *可批量赋值的字段
     */
    public $fillable = [
      'category_id',
      'admin_id',
      'product_id',
      'mer_id',
      'title',
      'author',
      'image_input',
      'synopsis',
      'share_title',
      'share_synopsis',
      'content',
      'visit',
      'sort',
      'url',
      'is_hide',
      'is_hot',
      'is_banner',
      'system_admin_id',
    ];


    /**
     *倒序默认
     */

    protected $defaultSortCriteria = ['id,desc'];

    /**
     * 可排序字段
     */
    public $sortable = [
        'id',
    ];
    /**
     * 可搜索的字段
     */

    public $searchable = [
      'id',
      'title',
      'image_input',
      'author',
      'synopsis',
      'category_id',
    ];
    /**
     * 查询字段模式
     *
     *  nickname => '%field%' 模糊查询
     *  created_at= (ge)field
     * @var array
     */
    public $searchableModels = [
        'title' => '%field%',
    ];

    /**
     * 文章可关联所属的分类
     */
    public function categories(){
            return $this->belongsTo(Category::class,'category_id','id');
    }

    /**
     * 文章可关联的所属的用户
     */
    public function admin(){
            return $this->belongsTo(SystemAdmin::class,'admin_id','id');
    }
    /**
     * 文章可关联的所属的商品
     */
    public function product(){
        return $this->belongsTo(Product::class,'product_id','id');
    }
    /**
     * 文章可关联所属的商户
     */
    public function mer(){
        return $this->belongsTo(MerchantRegister::class,'mer_id','id');
    }
}




